import Utils from "../../utils/Utils.js";
import Api from "../../api/Api.js";
import Algo from "../../utils/Algorithm.js";
import Db from "../../db/Db.js";
import Type from "../../db/Type.js";
export default class ServiceUtils {
    constructor() {
        if (ServiceUtils.instance) {
            return ServiceUtils.instance;
        }
        ServiceUtils.instance = this;
        this.utils = new Utils();
        this.api = new Api();
        this.algo = new Algo();
        this.db = new Db();
    }

    // 接口返回数据 组装成树结构
    async dataTreeFormat() {
        const meta_id = this.utils.getUrlParamer('meta_id')
        const res_person = await this.api.tree_data(meta_id,'person')
        const res_sensor = await this.api.tree_data(meta_id,'sensor')
        const res_camera = await this.api.tree_data(meta_id,'camera')
        // 人员分站列表
        const m_station_list = res_person.data.data.station_list
        // 人员列表
        const m_staff_list = res_person.data.data.staff_list
        // 工业视频列表
        const cameraList = res_camera.data.data.camera_list
        // 监控分站列表
        const k_station_list = res_sensor.data.data.station_list
        // 传感器测点列表
        const k_node_list = res_sensor.data.data.node_list
        // 从本地获取 设备信息
        const deviceDb = await this.db.operation(Type.Device,'getAll')
        const locatedMstation = deviceDb.filter((item) => {
            return item.sub1 == 0
        })
        const locatedKstation = deviceDb.filter((item) => {
            return item.sub1 == 1
        })
        const locatedKnode = deviceDb.filter((item) => {
            return item.sub1 == 2
        })
        const locatedVcamera = deviceDb.filter((item) => {
            return item.sub1 == 3
        })
        // 判断 是否被设置过位置
        m_station_list.forEach((item) => {
            const flag = locatedMstation.some((el) => {
                return el.device_id == item.StationId
            })
            if(flag) {
                item.isPos = true
            }
            else {
                item.isPos = false
            }
        })
        cameraList.forEach((item) => {
            const flag = locatedVcamera.some((el) => {
                return el.device_id == item.cameraIndexCode
            })
            if(flag) {
                item.isPos = true
            }
            else {
                item.isPos = false
            }
        })
        k_station_list.forEach((item) => {
            const flag = locatedKstation.some((el) => {
                return el.device_id == item.StationID
            })
            if(flag) {
                item.isPos = true
            }
            else {
                item.isPos = false
            }
        })
        k_node_list.forEach((item) => {
            const flag = locatedKnode.some((el) => {
                return el.device_id == item.NodeID
            })
            if(flag) {
                item.isPos = true
            }
            else {
                item.isPos = false
            }
        })
        // 组装
        m_staff_list.forEach((staff) => {
            for(let index = 0; index < m_station_list.length; index++) {
                if(staff.CurStationId == m_station_list[index].StationId) {
                    m_station_list[index].children.push(staff)
                    break
                }
            }
        })
        k_node_list.forEach((node) => {
            for(let index = 0; index < k_station_list.length; index++) {
                if(node.StationID == k_station_list[index].StationID) {
                    k_station_list[index].children.push(node)
                    break
                }
            }
        })
        return {m_station_list,k_station_list,cameraList}
    }

    // 指定误差距离下的随机偏转
    enablePosDeviate(coordinate, distance) {
        const random_x_deviate = (Math.random() - 0.5) * 2 * distance
        const random_y_deviate = (Math.random() - 0.5) * 2 * distance
        const random_coordinate = [coordinate[0]+random_x_deviate, coordinate[1]+random_y_deviate]
        const cordiX = this.algo.denormalX(random_coordinate[0])
        const cordiY = this.algo.denormalY(random_coordinate[1])
        return [cordiX,cordiY]
    }

}